﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;

namespace WaiGaoQiaoWeb.Controllers
{
    [WaiGaoQiaoWeb.FilterSession.AuthorizeAttribute]
    public class InstockController : Controller
    {
        //
        // GET: /Instock/
        /// <summary>
        /// 食材主表
        /// </summary>
        //protected InstockGroupInfo InkgInfo = new InstockGroupInfo(true);
        /// <summary>
        /// 食材明细
        /// </summary>
        protected InstockDetailedInfo instockDetailedInfo = new InstockDetailedInfo(true);

        //protected ConsumablesInfo consuminfo = new ConsumablesInfo(true);
        //protected ConsumablesDetailedInfo consumdetailinfo = new ConsumablesDetailedInfo(true);

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetSupplier()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            SupplierInfo supinfo = new SupplierInfo(true);
            IList<SupplierInfo> suplist = supinfo.SelSupplierInfo(100, 1, "");
            foreach (SupplierInfo sup in suplist)
            {
                dic.Add(sup.ID, sup.Supplier_Name);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            selectList.Insert(0, new SelectListItem() { Text = "请选择...", Value = "0" });
            return selectList;
        }

        /// <summary>
        /// 读取食材入库明细
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public JsonResult GetDetailList(PageVM pvm)
        {
            if (string.IsNullOrEmpty(pvm.query))
            {
                pvm.query = "and OrderNo = ''";
            }
            string condition = pvm.query;//"and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = instockDetailedInfo.GetCountInstockDetailed(condition);

            IList<IngredientsDetailedAbstract> list = instockDetailedInfo.GetAllInstockDetailed(condition, pvm.rp, pvm.page);

            List<string> v;
            foreach (IngredientsDetailedAbstract ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.InstockDt.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(WarehouseInfo.GetWarehouseName(ida.Warehouse_ID));
                v.Add(GroupInfo.GetGroupNameById(ida.Group_ID));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.Count.ToString());
                v.Add(ida.Price.ToString());
                v.Add(ida.Amount.ToString());
                v.Add(SupplierInfo.GetSupplierName(ida.Supplier_ID));
                v.Add(ida.RecordDateTime.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.OperatorID.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }

        /// <summary>
        /// 读取食材入库根据批号
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public JsonResult GetDetailListByOrderNo(PageVM pvm)
        {
            string condition = "and OrderNo = '" + pvm.query + "'";
            JsonFlexiGridData jsonFlexiGridData = new JsonFlexiGridData();
            jsonFlexiGridData.page = pvm.page;
            jsonFlexiGridData.total = instockDetailedInfo.GetCountInstockDetailed(condition);

            IList<IngredientsDetailedAbstract> list = instockDetailedInfo.GetAllInstockDetailed(condition, pvm.rp, pvm.page);

            List<string> v;
            foreach (IngredientsDetailedAbstract ida in list)
            {
                #region
                v = new List<string>();
                v.Add(ida.ID.ToString());
                v.Add(ida.InstockDt.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(WarehouseInfo.GetWarehouseName(ida.Warehouse_ID));
                v.Add(GroupInfo.GetGroupNameById(ida.Group_ID));
                v.Add(Con_GoodsInfo.GetGoodsNameByID(ida.Goods_ID));
                v.Add(ida.Count.ToString());
                v.Add(ida.Price.ToString());
                v.Add(ida.Amount.ToString());
                v.Add(SupplierInfo.GetSupplierName(ida.Supplier_ID));
                v.Add(ida.RecordDateTime.ToString("yyyyMMdd", DateTimeFormatInfo.InvariantInfo));
                v.Add(ida.OperatorID.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = ida.ID.ToString(),
                    cell = v
                };
                jsonFlexiGridData.rows.Add(fgr);
            }
            return Json(jsonFlexiGridData);
        }
        /// <summary>
        /// 获取一条食材
        /// </summary>
        /// <returns></returns>
        public virtual IngredientsDetailedAbstract GetOneInstockDetailed(int id)
        {
            InstockDetailedInfo idinfo =
                (InstockDetailedInfo)instockDetailedInfo.GetAllInstockDetailed(" and ID = " + id.ToString())[0];
            return idinfo;
        }

        /// <summary>
        /// 保存食材入库
        /// </summary>
        /// <param name="idvm"></param>
        /// <returns></returns>
        public ActionResult SaveInstockDetail(InstockDetailedVM idvm)
        {
            int rowcount = 0;
            InstockDetailedInfo idinfo = Mapper.DynamicMap<InstockDetailedInfo>(idvm);
            if (ModelState.IsValid)
            {
                rowcount = instockDetailedInfo.SaveInstockDetailed(idinfo);
            }
            return Json(rowcount);
        }
        /// <summary>
        /// 删除食材入库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DeleteInstockDetail(int id)
        {
            int rowcount = 0;
            rowcount = instockDetailedInfo.DeleteInstockDetailedWC("ID = " + id.ToString(), id);
            return Json(rowcount);
        }
        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetWarehouse()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            WarehouseInfo wareinfo = new WarehouseInfo(true);
            IList<WarehouseInfo> warelist = wareinfo.SelWarehouseInfo(100, 1, "");
            dic.Add(-1, "请选择...");
            foreach (WarehouseInfo wi in warelist)
            {
                dic.Add(wi.ID, wi.Warehouse_Name);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }
        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroup()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            GroupInfo ginfo = new GroupInfo(true);
            IList<GroupInfo> grouplist = ginfo.SelGroupInfo(100, 1, "");
            dic.Add(-1, "请选择...");
            foreach (GroupInfo gi in grouplist)
            {
                dic.Add(gi.ID, gi.Group_Name);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 检查供应商不能为请选择
        /// </summary>
        /// <param name="Supplier_ID"></param>
        /// <returns></returns>
        public ActionResult CheckSupplier_ID(int Supplier_ID)
        {
            bool istongguo = Supplier_ID == 0 ? false : true;
            return Json(istongguo);
        }

        /// <summary>
        /// 检查仓库不能为请选择
        /// </summary>
        /// <param name="Supplier_ID"></param>
        /// <returns></returns>
        public ActionResult CheckWarehouse_ID(int Warehouse_ID)
        {
            bool istongguo = Warehouse_ID == -1 ? false : true;
            return Json(istongguo);
        }

        /// <summary>
        /// 检查班组不能为请选择
        /// </summary>
        /// <param name="Supplier_ID"></param>
        /// <returns></returns>
        public ActionResult CheckGroup_ID(int Group_ID)
        {
            bool istongguo = Group_ID == -1 ? false : true;
            return Json(istongguo);
        }
    }
}
